package com.whz.eq;

import lombok.extern.slf4j.Slf4j;

import java.util.Objects;

/**
 * @ClassName CaseInsensitiveString
 * @Description TODO 大小写不敏感的字符串类
 * @Author hong-zhi
 * @Date 2021/5/3 12:52
 * @Version 1.0
 */

@Slf4j
public final class CaseInsensitiveString {
    private final String s;

    private CaseInsensitiveString(String s) {
        this.s = Objects.requireNonNull(s);
    }

    /*
    * 静态工厂方法
    * */
    public static CaseInsensitiveString create(String s) {
        return new CaseInsensitiveString(s);
    }

    @Override
    public boolean equals(Object o) {
        log.info("o.getClass() : {}", o.getClass());
        if(o instanceof CaseInsensitiveString) {
            log.info("调用了 o instanceof CaseInsensitiveString");
            return s.equalsIgnoreCase(((CaseInsensitiveString) o).s);
        }
        /*
        * 导致 CaseInsensitiveString 中的equals方法违反了对称性原则
        * */
//        if(o instanceof String) {
//            log.info("调用了 o instanceof String");
//            return s.equalsIgnoreCase((String) o);
//        }
        return false;
    }

    @Override
    public String toString() {
        return "CaseInsensitiveString{" +
                "s='" + s + '\'' +
                '}';
    }
}
